package com.collectplus.express.order;

import android.app.Activity;
import android.graphics.Paint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.collectplus.express.R;
import com.collectplus.express.model.Const;
import com.collectplus.express.model.CouponBean;
import com.collectplus.express.model.OrderBean;
import com.collectplus.express.model.OrderPriceBean;
import com.collectplus.express.model.VisitDoorBackBean;

import droid.frame.activity.base.BaseAdapterExt;
import droid.frame.utils.lang.Str;

/**
 * Created by joker on 2017/12/18.
 */

public class OrderPriceVisitHomeAdapter extends BaseAdapterExt<OrderPriceBean> {

    private CouponBean coupon;

    // private String totalPrice;

    public OrderPriceVisitHomeAdapter(OrderBean order, Activity context) {
        super(null, context);
        items.clear();
        OrderPriceBean price1 = new OrderPriceBean();
        VisitDoorBackBean visitDoorBackBean = order.getResultObject();
        if (null == visitDoorBackBean) {
            price1.setPrice("0.00");
            price1.setDiscount("");
            price1.setType("");
            price1.setDiscountType(1000);
            price1.setParcelType(Const.ParcelType.visitToHome);
        } else {
            price1.setPrice(visitDoorBackBean.getPrice());
            price1.setDiscount(visitDoorBackBean.getDiscount());
            price1.setType(visitDoorBackBean.getType());
            price1.setDiscountType(visitDoorBackBean.getDiscountType());
            price1.setParcelType(Const.ParcelType.visitToHome);
        }
        price1.setName("上门费");
        items.add(price1);
        // 如果上门服务费 > 0
        if (Str.parseFloat(order.getServiceCost()) > 0) {
            OrderPriceBean price2 = new OrderPriceBean();
            price2.setName("上门服务费");
            price2.setPrice(order.getServiceCost());
            items.add(price2);
        }
        // totalPrice = MathUtils.add(price1.getPrice(), price2.getPrice()) + "";
        setItems(items, false);
    }

    @Override
    public int getCount() {
        return super.getCount() + (coupon != null ? 1 : 0);
    }

    public void setCoupon(CouponBean coupon) {
        this.coupon = coupon;
        notifyDataSetChanged();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.pay_price_item, parent, false);
            holder = new ViewHolder();
            holder.icon = convertView.findViewById(R.id.order_price_icon);
            holder.name = (TextView) convertView.findViewById(R.id.order_price_name);
            holder.price = (TextView) convertView.findViewById(R.id.order_price_money);
            holder.discountDesc = (TextView) convertView.findViewById(R.id.discountdesc_tv);
            holder.help = (ImageView) convertView.findViewById(R.id.help_iv);
            holder.period = (TextView) convertView.findViewById(R.id.order_period_tv);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        OrderPriceBean item = getItem(position);
        holder.name.setText(item.getName());
        //上门
        holder.help.setVisibility(View.INVISIBLE);
        if (!Str.isEmpty(item.getDiscount())) {
            holder.discountDesc.setVisibility(View.VISIBLE);
            holder.discountDesc.setText(item.getDiscount());
        }
        if (!Str.isEmpty(item.getType())) {
            holder.period.setVisibility(View.VISIBLE);
            holder.period.setText("（" + item.getType() + "）");
        }
        //进行画线,限免
        if (1 == item.getDiscountType()) {
            holder.price.getPaint().setAntiAlias(true);//抗锯齿
            holder.price.setPaintFlags(Paint.STRIKE_THRU_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG);
            holder.price.setText("￥" + item.getPrice());
        } else {
            holder.price.setText("￥" + item.getPrice());
        }

        if (position > 0) {
            holder.icon.setVisibility(View.INVISIBLE);
        } else {
            holder.icon.setVisibility(View.VISIBLE);
        }
        return convertView;
    }

    private class ViewHolder {
        private View icon;
        private TextView name;
        private TextView price;
        private TextView discountDesc;
        private ImageView help;
        private TextView period;
    }
}
